package com.cakeshop.servelt;

import com.cakeshop.model.Comment;
import com.cakeshop.service.CommentService;
import com.cakeshop.service.impl.CommentServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * 负责评论中回复的处理
 *
 * @author nineice
 */
@WebServlet(name = "UserCommentReplyServlet", value = "/user_reply")
public class UserCommentReplyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String replyId = request.getParameter("replyId");
        Comment reply = new Comment(
                request.getParameter("username"),
                request.getParameter("commitDate"),
                request.getParameter("commitContent")
        );

        // 回复评论
        CommentService commentService = new CommentServiceImpl();
        commentService.appendReply(Integer.parseInt(replyId), reply);
        request.setAttribute("commentList", commentService.getAllComment());

        // 从哪来回哪去
        response.sendRedirect(request.getHeader("Referer"));
    }
}
